<?php
/**
 * ApplicationLogger class file
 * @package Facebook
 */

/**
 * This class logs each add and remove of the appliation.
 * 
 * @package Facebook
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @version 1.0.1
 * @date 10/7/07
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */
class ApplicationLogger {
	
	/**
	 * Gets the number of installs since a specific time.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param string $since The time to get installs from
	 * @return int The number of installs
	 */
	static public function get_installs($since) {
		$query = sprintf(
			"SELECT COUNT(*) FROM %s_logs WHERE time > '%s' AND action='add'",
			DB_TABLE,
			ApplicationLogger::sql_clean($since)
		);
		$result = mysql_query($query);
		while ($row = mysql_fetch_array($result)) {
			$installs = $row['COUNT(*)'];
		}
		return $installs;
	}
	
	/**
	 * Gets the last stats update.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @return The row if the query was successful
	 */
	static public function get_last_stats() {
		$query = sprintf(
			"SELECT time, users FROM %s_stats ORDER BY time DESC LIMIT 1",
			DB_TABLE
		);
		$result = mysql_query($query);
		if ($row = mysql_fetch_assoc($result)) {
			return $row;
		}
		return false;
	}
	
	/**
	 * Gets the number of removes since a specific time.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param string $since The time to get removes from
	 * @return int The number of removes
	 */
	static public function get_removes($since) {
		$query = sprintf(
			"SELECT COUNT(*) FROM %s_logs WHERE time > '%s' AND action='rem'",
			DB_TABLE,
			ApplicationLogger::sql_clean($since)
		);
		$result = mysql_query($query);
		while ($row = mysql_fetch_array($result)) {
			$removes = $row['COUNT(*)'];
		}
		return $removes;
	}
	
	/**
	 * Logs an install of the application.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param int $uid The uid of the adding user
	 * @return boolean True if the query was successful
	 */
	static public function log_install($uid) {
		$query = sprintf(
			"INSERT INTO %s_log (uid, action) VALUES ('%d', 'add')",
			DB_TABLE,
			ApplicationLogger::sql_clean($uid)
		);
		mysql_query($query) or die();
		return true;
	}
	
	/**
	 * Logs a news feed notification.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param int $uid The uid of the user being notified
	 * @param string $type The type of notification, either photos or photosets
	 * @return boolean True if the query was successful
	 */
	static public function log_notification($uid, $type, $success, $extra = '') {
		$type = ($type == 'photosets') ? 'nfs' : 'nfp';
		$success = ($success) ? '1' : '0';
		$query = sprintf(
			"INSERT INTO %s_log (uid, action, success, extra) VALUES ('%d', '%s', '%d', '%s')",
			DB_TABLE,
			ApplicationLogger::sql_clean($uid),
			$type,
			$success,
			ApplicationLogger::sql_clean($extra)
		);
		mysql_query($query) or die();
		return true;
	}
	
	/**
	 * Logs a removal of the application.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param int $uid The uid of the removing user
	 * @return boolean True if the query was successful
	 */
	static public function log_remove($uid, $success = true) {
		$success = ($success) ? '1' : '0';
		$query = sprintf(
			"INSERT INTO %s_log (uid, action, success) VALUES ('%d', 'rem', '%d')",
			DB_TABLE,
			ApplicationLogger::sql_clean($uid),
			$success
		);
		mysql_query($query) or die();
		return true;
	}
	
	/**
	 * Logs a profile update.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param int $uid The uid of the updating user
	 * @return boolean True if the query was successful
	 */
	static public function log_update($uid) {
		$success = ($success) ? 'true' : 'false';
		$query = sprintf(
			"INSERT INTO %s_log (uid, action) VALUES ('%d', 'upd')",
			DB_TABLE,
			ApplicationLogger::sql_clean($uid)
		);
		mysql_query($query) or die();
		return true;
	}
	
	/**
	 * Records the stats for a given time.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param int $installs The number of installs
	 * @param int $removes The number of removes
	 * @param int $the number of users
	 * @return boolean True if the query was successful
	 */
	static public function record_stats($installs, $removes, $users) {
		$query = sprintf(
			"INSERT INTO %s_stats (time, installs, removes, users) VALUES (now(), '%d', '%d', '%d')",
			DB_TABLE,
			$installs,
			$removes,
			$users
		);
		mysql_query($query) or die();
		return true;
	}
	
	/**
	 * Sanitizes information to be put in the database.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param string $value The value to clean
	 * @return string The SQL-safe value
	 */
	static public function sql_clean($value) {
		if (get_magic_quotes_gpc()) {
			if (ini_get('magic_quotes_sybase')) {
				$value = str_replace("''", "'", $value);
			}
			else {
				$value = stripslashes($value);
			}
		}
		if (function_exists("mysql_real_escape_string")) {
			$value = mysql_real_escape_string($value);
		}
		return $value;
	}
	
}

?>